<?php
/*! \brief
 * This is WFDB abstract class
 * \author William Su
 */
class ADB
{
	/**
	 * @var WF
	 */
	protected $objFramework;
	/**
	 * @var WFWidgetPoolFactory
	 */
	protected $objWidgetPoolFactory;
	/**
	 * @var WFDatabaseObject
	 */
	protected $objDataObject;
	//private $objPropelPDO;
	public function __construct($objFramework){
		$this->objFramework = $objFramework;
		$this->objWidgetPoolFactory = $this->objFramework->getWidgetPoolFactory();
		$this->objDataObject = $this->objFramework->getDataObject();
	}
	/**
	 *\brief Description
	 *  - setter of $objDataObject (WFDataObject)
	 */
	public function setObjDataObject($objDataObject){
	    $this->objDataObject = $objDataObject;
	}
	
	/**
	 *\brief Description
	 *  - This method will initial create/drop/truncate All table of Widget Framework
	 *  - If success return true, If failed will raised Exception
	 *\par Variable
	 *   - EX : $mixTableName string
	 *   WP_UPDATE_LOG
	 *   - EX : $mixTableName array
	 *   array (
	 *  	0 => 'WIDGET',
	 *  	1 => 'WIDGET_CATEGORY'
	 *	)
	 *	- EX : $defaultArrTable array
	 *	array (
	 *  	0 => 'WIDGET',
	 *  	1 => 'WIDGET_CATEGORY'
	 *  	2 => 'WIDGET_CATEGORY_MAPPING'
	 *  	3 => 'WP_UPDATE_FILTER'
	 *  	4 => 'WP_UPDATE_LOG'
	 *  	5 => 'proxylist'
	 *  	6 => 'serverlist'
	 *  	7 => 'serverlisttype'
	 *  	8 => 'userdata'
	 *  	9 => 'users'
	 *	)
	 *	- EX : $strQueryType create,drop,truncate
	 */
	protected function createDropTruncateDBSchema($defaultArrTable, $strQueryType, $mixTableName = null){
		if(null === $mixTableName){
			$arrTableName = $defaultArrTable;
		}else{
			if(is_array($mixTableName) && $mixTableName){
				$arrTableName = $mixTableName;
			}elseif (is_string($mixTableName)){
				$arrTableName[] = $mixTableName;
			}
		}
		
		$isSuccess = true;
		if(is_array($arrTableName) && $arrTableName){
		    $arrFailTable = array();
    		foreach($arrTableName as $strTableName){
    			$isTableExisted = $this->objDataObject->isTableExists($strTableName);
    			if(($strQueryType === 'create' && false === $isTableExisted) || ($strQueryType === 'drop' && true === $isTableExisted) || ($strQueryType === 'truncate' && true === $isTableExisted)){
    			    switch($strQueryType){
    			        case 'create':
    			            $strFuncName = 'getCreateTable'.$this->objFramework->getTypeFactory()->getString()->getUpperCamelCase($strTableName);
    			        break;
    			        case 'drop':
    			            $strFuncName = 'getDropTable'.$this->objFramework->getTypeFactory()->getString()->getUpperCamelCase($strTableName);
    			        break;
    			        case 'truncate':
    			            $strFuncName = 'getTruncateTable'.$this->objFramework->getTypeFactory()->getString()->getUpperCamelCase($strTableName);
    			        break;
    			        default:
    			            $strMsg = __CLASS__.'::'.__FUNCTION__.'()=>Please input correct $strQueryType';
    			            throw new WFDbDBException($strMsg);
    			        break;
    			    }
    			    mydebug_log($strFuncName);
					if(method_exists($this->objFramework->getDbFactory()->getSqlGen(),$strFuncName)){
    					$sql = $this->objFramework->getDbFactory()->getSqlGen()->$strFuncName();
    					$isSuccessTmp = $this->objDataObject->queries($sql);
					}else{
					    $isSuccessTmp = false;
					}
					if(false === $isSuccessTmp){
					    $arrFailTable[] = $strTableName;
					}
					$isSuccess = $isSuccess && $isSuccessTmp;
    			}
    		}
		}
		if(true === $isSuccess){
			return $isSuccess;
		}else{
			$message = __CLASS__.'::'.__FUNCTION__."()=>Widget Framework ".$strQueryType." table [".implode(',',$arrFailTable)."] Failed!";
			throw new WFDbDBException($message);
		}
	}
}
